package team.bluepen.supermarket.data.entity;

import team.bluepen.supermarket.constant.Role;
import team.bluepen.supermarket.data.dao.abstraction.Buildable;
import team.bluepen.supermarket.util.StringUtil;

import javax.annotation.Nonnull;
import java.io.Serializable;
import java.util.Objects;

/**
 * 用户对象
 * @author Kai
 */
public class User implements Serializable, HbaseItem<User>, Buildable<User> {
    /** Hbase中存储的Row ID */
    private @Nonnull String row;

    /** 用户名 */
    private String username;

    /** 用户密码 */
    private String password;

    /** 用户身份 */
    private Role role;

    /** 用户ID */
    private long id;

    public User(){
        row = "";
    }

    public User(@Nonnull String row, String username, String password, long id, Role role) {
        this.row = row;
        this.username = username;
        this.password = password;
        this.id = id;
        this.role = role;
    }

    public User(@Nonnull String row, String username, String password, long id, String role) {
        this.row = row;
        this.username = username;
        this.password = password;
        this.id = id;
        this.role = Role.get(role);
    }

    public String getUsername() {
        return username;
    }

    public User setUsername(String username) {
        this.username = username;
        return this;
    }

    public String getPassword() {
        return password;
    }

    public User setPassword(String password) {
        this.password = password;
        return this;
    }

    public long getId() {
        return id;
    }

    public User setId(long id) {
        this.id = id;
        return this;
    }

    public Role getRole() {
        return role;
    }

    public User setRole(Role role) {
        this.role = role;
        return this;
    }

    @Nonnull
    public String getRow() {
        return row;
    }

    public User setRow(@Nonnull String row) {
        this.row = row;
        return this;
    }

    public User generateRowKey() {
        // 不需要多Hbase服务器负载均衡的情况，直接使用id作为row key
        String rowKey = "";
        if (check()) {
            rowKey = StringUtil.convert(id, 7);
        }
        row = rowKey;
        return this;
    }

    private boolean check() {
        return id != 0 && password != null
                && username != null && role != null;
    }

    @Override
    public String toString() {
        return "User{" +
                "row='" + row + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", role=" + role +
                ", id=" + id +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return id == user.id && row.equals(user.row)
                && Objects.equals(username, user.username)
                && Objects.equals(password, user.password)
                && role == user.role;
    }

    @Override
    public int hashCode() {
        return Objects.hash(row, username, password, role, id);
    }

    @Override
    public User build() {
        return this;
    }
}
